set ansi_padding, ansi_warnings, concat_null_yields_null, arithabort, quoted_identifier, ansi_nulls on
go

if exists (select * from tempdb..sysobjects where id=object_id('tempdb..#tmpProperties')) drop table #tmpProperties
go

create table #tmpProperties ( name nvarchar(255), value nvarchar(255) )
go

insert into #tmpProperties values('script', 'update.0005.sql')
insert into #tmpProperties values('error', '0')

set xact_abort on
go

set transaction isolation level serializable
go

begin transaction
go

set ansi_nulls on
set quoted_identifier on
go
/* BEGIN UPDATE SCRIPT */



/*
 Topic view:
 - add terminogoly
 - Gridview: Add description column
 - Remove Id column
*/

INSERT INTO [dbo].[GRIDVIEWS_COLUMNS]
           ([ID]
           ,[DELETED]
           ,[CREATED_BY]
           ,[DATE_ENTERED]
           ,[MODIFIED_USER_ID]
           ,[DATE_MODIFIED]
           ,[DATE_MODIFIED_UTC]
           ,[GRID_NAME]
           ,[COLUMN_INDEX]
           ,[COLUMN_TYPE]
           ,[DEFAULT_VIEW]
           ,[HEADER_TEXT]
           ,[SORT_EXPRESSION]
           ,[ITEMSTYLE_WIDTH]
           ,[ITEMSTYLE_CSSCLASS]
           ,[ITEMSTYLE_HORIZONTAL_ALIGN]
           ,[ITEMSTYLE_VERTICAL_ALIGN]
           ,[ITEMSTYLE_WRAP]
           ,[DATA_FIELD]
           ,[DATA_FORMAT]
           ,[URL_FIELD]
           ,[URL_FORMAT]
           ,[URL_TARGET]
           ,[LIST_NAME]
           ,[URL_MODULE]
           ,[URL_ASSIGNED_FIELD]
           ,[MODULE_TYPE]
           ,[PARENT_FIELD])
     VALUES
           ('8F477239-749D-4BAC-B6F8-1D38D2F8EA4A'
           ,0
           ,null
           ,getdate()
           ,null
           ,getdate()
           ,getdate()
           ,'Topics.ListView'
           ,2
           ,'TemplateColumn'
           ,0
           ,'Topics.LBL_LIST_DESCRIPTION'
           ,'DESCRIPTION'
           ,'35%'
           ,'listViewTdLinkS1'
           ,null
           ,null
           ,1
           ,'Description'
           ,null
           ,null
           ,null
           ,null
            ,null
           ,null
           ,null
           ,null
           ,null)
GO

update GRIDVIEWS_COLUMNS set DELETED=1 where ID='7C0AF8DA-EDF2-44CF-8119-041A338E3793'

INSERT INTO [dbo].[TERMINOLOGY] (ID,[NAME],[MODULE_NAME],[DISPLAY_NAME],[LANG],[DELETED],[DATE_ENTERED],[DATE_MODIFIED],[DATE_MODIFIED_UTC])
VALUES
	   ('B8790108-5FC2-408B-8DFA-752471B42C89'
	   ,'LBL_LIST_NAME'
	   ,'Topics'
	   ,'Name'
	   ,'en-US',0,getdate(),getdate(),getdate())
  
INSERT INTO [dbo].[TERMINOLOGY] (ID,[NAME],[MODULE_NAME],[DISPLAY_NAME],[LANG],[DELETED],[DATE_ENTERED],[DATE_MODIFIED],[DATE_MODIFIED_UTC])
VALUES
	   ('803EBD9A-EE13-4746-BCB6-8231390EA6BC'
	   ,'LBL_LIST_DESCRIPTION'
	   ,'Topics'
	   ,'Description'
	   ,'en-US',0,getdate(),getdate(),getdate())
	   
INSERT INTO [dbo].[TERMINOLOGY] (ID,[NAME],[MODULE_NAME],[DISPLAY_NAME],[LANG],[DELETED],[DATE_ENTERED],[DATE_MODIFIED],[DATE_MODIFIED_UTC])
VALUES
	   ('5097E17F-9F0B-4044-9545-929EC8DBFAB9'
	   ,'LBL_LIST_FORM_TITLE'
	   ,'Topics'
	   ,'Topic list'
	   ,'en-US',0,getdate(),getdate(),getdate())
	   
INSERT INTO [dbo].[TERMINOLOGY] (ID,[NAME],[MODULE_NAME],[DISPLAY_NAME],[LANG],[DELETED],[DATE_ENTERED],[DATE_MODIFIED],[DATE_MODIFIED_UTC])
VALUES
	   ('16248E99-22F6-4768-807E-E198CBAFFC45'
	   ,'LNK_TOPIC_LIST'
	   ,'Topics'
	   ,'Topics'
	   ,'en-US',0,getdate(),getdate(),getdate())

INSERT INTO [dbo].[TERMINOLOGY] (ID,[NAME],[MODULE_NAME],[DISPLAY_NAME],[LANG],[DELETED],[DATE_ENTERED],[DATE_MODIFIED],[DATE_MODIFIED_UTC])
VALUES
	   ('9232DD54-3E3D-44FD-9A1C-AF7410C1AA51'
	   ,'LNK_NEW_TOPIC'
	   ,'Topics'
	   ,'Create Topic'
	   ,'en-US',0,getdate(),getdate(),getdate())

INSERT INTO [dbo].[TERMINOLOGY] (ID,[NAME],[MODULE_NAME],[DISPLAY_NAME],[LANG],[DELETED],[DATE_ENTERED],[DATE_MODIFIED],[DATE_MODIFIED_UTC],[LIST_NAME],LIST_ORDER)
VALUES
	   ('BA0389BD-4FF9-4FE6-A0BC-ABC3C17EFB8D'
	   ,'Topics'
	   ,null
	   ,'Topics'
	   ,'en-US',0,getdate(),getdate(),getdate()
	   ,'moduleList'
	   ,50)	   




/* END UPDATE SCRIPT */
if @@error<>0 and @@trancount>0 rollback transaction
go

if @@trancount=0 begin
	update #tmpProperties set value = 1 where name = 'error'
	begin transaction
end
go

if (select top 1 value from #tmpProperties where name = 'error') <> '0' rollback transaction
go

if @@trancount > 0 begin
	declare @@scriptName nvarchar(255)
	select top 1 @@scriptName = value from #tmpProperties where name = 'script'

	if dbo.scriptExisted(@@scriptName) = 1 begin
		rollback transaction
		print @@scriptName + ' already executes'
	end
	else begin
		exec dbo.updateScript @@scriptName
		commit transaction
		print 'The database update succeeded'
	end
END
else begin
	print 'The database update failed'
end
go

drop table #tmpProperties
go
